package server;

import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.DatagramChannel;
import java.util.HashMap;

import org.hsqldb.server.Server;

import obj.Piped;


/**
 * Class Main Server instance
 */
public class Oserver {

	//IP / Port configuration
    private volatile static int serverPort = 5554 ;
    private volatile static String serverIp = "localhost";

    //UDP packet cannot received more than 65508 bytes
	public static final int buffsize = 1024;
	
	private final int NB_MAX_CLIENT = 0 ;
	private volatile int thisNbMax = NB_MAX_CLIENT - 1 ; 
    //Define server port
	
    //server channel
    private DatagramChannel udpchannel ;
    
    //Database instance
    private Server database = new Server(); 

//    private String database_path  = ""; 
    //Count the number of mobile asking for positioning, volatile is not appropriate...
    private volatile static int nbMobileThread = 0 ;

    // HashMap store the socketaddress, inputstream and outputstream of all threads and
	private HashMap<SocketAddress,Piped>  lOfThreads = new HashMap<SocketAddress, Piped>();

    
    /**
     * Constructor Server    
     */
    public Oserver(){
        System.out.println("S : Server initialisation...");
    }

    /**
     * Setter increasing by 1 the number of threads
     * 
     */
    public static void incNbMobileThread(){
    	nbMobileThread++;
    }
    /**
     * Setter decreasing by 1 the number of threads
     */
    public static void decNbMobileThread(){
    	nbMobileThread--;
    }
    /**
     * Getter of the number of threads
     */
    public static int getNbMobileThread(){
        return nbMobileThread;
    }

    /**
     * Get server port
     */
    public static int getPort(){
        return serverPort;
    }
    /**
     * Get server port
     */
    public static void setPort(int serverPort){
        Oserver.serverPort = serverPort;
    }
    
	public int getThisNbMax() {
		return this.thisNbMax;
	}

    /**
     * Create and get Server InetAddress
     * @return InetAddress
     */
    public static InetAddress getInetAddress(){
        // Get Inet Address 127.0.0.1
    	InetAddress hostAddress = null;
        try {
            hostAddress = InetAddress.getByName(serverIp);
        } catch (UnknownHostException e) {
            System.out.println("S : Could not get server IP : "+ hostAddress);
            e.printStackTrace();
        }   
        return hostAddress ;
    }

    /**
     * Set the list of Threads in order to manage them
     * @param HashMap<SocketAddress,Piped> lOfThreads
     */
	public  void setlOfThreads(HashMap<SocketAddress,Piped> lOfThreads) {
		this.lOfThreads = lOfThreads;
	}

	/**
	 * Get the list of Threads in order to manage them
     * @param HashMap<SocketAddress,Piped> lOfThreads
     */
	public HashMap<SocketAddress,Piped> getlOfThreads() {
		return lOfThreads;
	}

	public  void setDatabase(Server database) {
		this.database = database;
	}

	public  Server getDatabase() {
		return database;
	}

	public  void setUdpchannel(DatagramChannel udpchannel) {
		this.udpchannel = udpchannel;
	}

	public  DatagramChannel getUdpchannel() {
		return udpchannel;
	}
}